<template>
  <div class="login-container">
    <!-- 背景图遮罩层 -->
    <div class="bg-overlay"></div>
    
    <!-- 居中登录卡片 -->
    <div class="login-card">
      <div class="card-header">
        <h1>欢迎回来</h1>
        <span class="tip_1">——账号密码登录——</span>
      </div>
      <!-- 14.讲解 -->
      <el-form ref="formRef" :model="form" :rules="rules" class="login_form">
        <el-form-item prop="id">
          <el-input v-model="form.id" placeholder="请输入账号">
            <template #prefix>
              <el-icon><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            type="password"
            show-password
            v-model="form.password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            round
            color="#626aef"
            @click="onSubmit($route.params.type)"
            :loading="loading"
            class="login-btn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

import { toast } from "~/composables/util";
const store = useStore();
const form = reactive({
  id: "",
  password: "",
});
const formRef = ref(null);
const loading = ref(false);
const router = useRouter();

const onSubmit = (type) => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return false;
    } else {
      loading.value = true;
      store.dispatch("login",{id:form.id,password:form.password,type:type}).then(async(res)=>{
        toast("登陆成功","success")
        router.push("/home")
        await store.dispatch("getinfo")
        console.log(store.state.user);
      }).finally(() => {
        loading.value = false;
      })
    }
  });
};

const rules = {
  id: [
    {
      required: true,
      message: "账号不能为空",
      trigger: "blur",
    },
    {
      min: 3,
      max: 12,
      message: "账号长度在3-12位之间",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
    {
      min: 5,
      max: 15,
      message: "密码长度须为5-15位",
      trigger: "blur",
    },
  ],
};

// 监听回车事件
function onKeyUp(e) {
  if (e.key === "Enter") {
    onSubmit($route.params.type);
  }
}
// 5讲解
// 添加键盘监听
onMounted(() => {
  document.addEventListener("keyup", onKeyUp);
});

// 移除键盘监听
onBeforeUnmount(() => {
  document.removeEventListener("keyup", onKeyUp);
});
</script>

<style scoped>
/* 全局样式重置 */
body {
  margin: 0;
  padding: 0;
  height: 100vh;
  overflow: hidden;
}

/* 登录容器 */
.login-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 背景图 */
.bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('https://picsum.photos/id/24/1920/1080'); /* 图书背景图 */
  background-size: cover;
  background-position: center;
  filter: brightness(0.7); /* 降低亮度，使文字更清晰 */
  z-index: -1;
}

/* 登录卡片 */
.login-card {
  background-color: rgba(255, 255, 255, 0.50); /* 半透明背景 */
  padding: 40px 50px;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  width: 380px;
  max-width: 90%;
  transition: transform 0.3s ease;
}

/* 卡片悬停效果 */
.login-card:hover {
  transform: translateY(-5px);
}

/* 卡片头部 */
.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.card-header h1 {
  color: #333;
  font-size: 28px;
  margin-bottom: 10px;
}

.tip_1 {
  color: #666;
  font-size: 14px;
}

/* 表单样式 */
.login_form {
  width: 100%;
}

/* 输入框样式 */
.el-input {
  height: 44px;
}

.el-input__inner {
  height: 44px;
  border-radius: 8px;
  padding-left: 40px;
  border-color: #e0e0e0;
}

.el-input__prefix {
  left: 12px;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  background-color: #6d6b54 !important;
  color: white !important;
  transition: all 0.3s ease;
}

.login-btn:hover {
  background-color: #6d6b54 !important;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(101, 106, 69, 0.3);
}
/* 1.讲解 */
/* 响应式设计 */
@media (max-width: 576px) {
  .login-card {
    padding: 30px 35px;
    width: 90%;
  }
  
  .card-header h1 {
    font-size: 24px;
  }
}
</style>